PHP HTTP Clients and on the fly gzip compression.
2009 July 5 at 06:00 » Tagged as :deflate, gzip,
<?
echo file_get_contents("compress.zlib://http://raditha.com/", "r");
?>
<?
$zp = gzopen("http://raditha.com/", "r");
gzpassthru($zp);
?>
fwrite($this->socket, "Accept-Encoding: gzip,deflatern");
$this->headerArray = split("rn", $this->headers); foreach($this->headerArray as $head) { $parts = split(": ",$head); if(strtolower($parts[0]) == 'content-encoding: gzip') { $gzip=true; } }It's not a requirement to convert the headers to lower case before comparision; we are just playing safe because sometime you find weird servers not capitalizing the second word in the header. Once you have identified that the content is gzipped, you might be tempted to just use the
gzinflate
() method to inflate what you read in from the socket.
gzinflate(stream_get_contents($this->socket, $length-10));
PHP Warning: gzinflate(): data error in /var/www/clients/twitter/RadHTTPClient.php on line 261 PHP Stack trace: PHP 1. {main}() /var/www/raditha/http-deflate.php:0 PHP 2. RadHTTPClient->getMessageBody() /var/www/raditha/http-deflate.php::20 PHP 3. gzinflate() /var/www/raditha/RadHTTPClient.php:261
It happens because thegzinflate
() function doesn't expect to deal with the gzip header. The header contains the gzip magic number as well as other flags such as file modification time etc. This header has to be stripped out before you pass it into the gzinflate
function.
if($useGzip) { $b = fread($this->socket,2); if(ord($b[0]) == 0x1f && ord($b[1]) == 0x8b) { $gzHead = fread($this->socket,8); $s = gzinflate(stream_get_contents($this->socket, $length-10)); } }if the magic number 0x1f8b is not found you can treat the data as already uncompressed data (of course you will need to add the 'else' to go with it). One last thing; if you want to enable one the fly gzip compression with Apache all you need to do is to add the following directives to the httpd.conf file
LoadModule deflate_module modules/mod_deflate.so
AddOutputFilterByType DEFLATE text/html text/plain text/xml